package edu.pku.sei.transformation.editor.model.execution;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Element;

import edu.pku.sei.modeler.model.xml.XMLUtility;
import edu.pku.sei.transformation.editor.model.ModelPackage;
import edu.pku.sei.transformation.editor.model.base.NamedElement;
import edu.pku.sei.transformation.editor.model.constraint.ConstraintConnection;
import edu.pku.sei.transformation.editor.model.constraint.IConstrainableElement;

public class Node extends NamedElement implements INode, IConstrainableElement {
	private ArrayList outgoings = new ArrayList();
	private ArrayList incomings = new ArrayList();
	private ArrayList<ConstraintConnection> constraintConnections = new ArrayList<ConstraintConnection>();

	public ArrayList getOutgoings() {
		return outgoings;
	}

	public void setOutgoings(ArrayList outgoings) {
		this.outgoings = outgoings;
	}

	public ArrayList getIncomings() {
		return incomings;
	}

	public void setIncomings(ArrayList incomings) {
		this.incomings = incomings;
	}

	@Override
	public void serialize(Element root) {
		super.serialize(root);
		XMLUtility.xmlInstance.serializeNodes(root,
				ModelPackage.NODE_OUTGOINGS, this.outgoings);
		XMLUtility.xmlInstance.serializeNodesNotContain(root,
				ModelPackage.NODE_INCOMINGS, this.incomings);
		XMLUtility.xmlInstance.serializeNodes(root,
				ModelPackage.CONSTRAINT_CONNECTION, this.constraintConnections);
	}

	@Override
	public void deSerialize(Element root) {
		super.deSerialize(root);
		this.outgoings.addAll(XMLUtility.xmlInstance.deSerializeNodes(root,
				ModelPackage.NODE_OUTGOINGS));
		this.incomings.addAll(XMLUtility.xmlInstance
				.deSerializeNodesNotContain(root, ModelPackage.NODE_INCOMINGS,
						this));
		this.constraintConnections.addAll(XMLUtility.xmlInstance
				.deSerializeNodes(root, ModelPackage.CONSTRAINT_CONNECTION));
	}

	@Override
	public void dynamicSet(String featureID, Object value) {
		if (ModelPackage.NODE_INCOMINGS.equals(featureID)) {
			this.incomings.add(value);
		} else if (ModelPackage.NODE_OUTGOINGS.equals(featureID)) {
			this.outgoings.add(value);
		} else
			super.dynamicSet(featureID, value);
	}

	@Override
	public List<ConstraintConnection> getConstraintConnections() {
		return constraintConnections;
	}
}
